package com.minhua.jijiehao2.mainframe.launcher.forgetpasswordactivity.impl

import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.mainframe.launcher.finalforgetpasswordactivity.impl.FinalForgetPasswordActivity
import com.minhua.jijiehao2.mainframe.launcher.forgetpasswordactivity.imp.IForgetPasswordView
import com.minhua.jijiehao2.utils.T
import kotlinx.android.synthetic.main.activity_forget_password.*

internal class ForgetPasswordActivity : BaseActivity<IForgetPasswordView, ForgetPasswordPresenter>(), IForgetPasswordView {
    override fun getLayoutId(): Int = R.layout.activity_forget_password

    override fun getPresenter(): ForgetPasswordPresenter = ForgetPasswordPresenter()

    override fun onDestroy() {
        super.onDestroy()
        code.endTimer()
    }

    companion object {
        fun intentStart(context: Context, phone: String) {
            val intent = Intent(context, ForgetPasswordActivity::class.java)
            intent.putExtra("phone", phone)
            context.startActivity(intent)
        }
    }

    private val textWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

        override fun afterTextChanged(s: Editable?) {
            tvNext.isEnabled = !(TextUtils.isEmpty(phone.text) || TextUtils.isEmpty(code.text))
        }
    }

    override fun initView() {
        var phoneNum = intent.getStringExtra("phone")
        phone.editText.setText(phoneNum)
        code.setOnCodeSendListener { mPresenter.sendCode() }
        phone.editText.addTextChangedListener(textWatcher)
        code.editText.addTextChangedListener(textWatcher)
        tvNext.setOnClickListener { FinalForgetPasswordActivity.intentStart(this, getUserName(), getCheckCode()) }
    }

    override fun getUserName(): String = phone.text.toString().trim()

    override fun getCheckCode(): String = code.text.toString().trim()


    override fun onUserNameError() {
        T.showShort("请输入正确的手机号")
    }

    override fun onCheckCodeError() {
        T.showShort("验证码错误")
    }

    override fun onSendCodeSuccess(msg: String) {
        T.showShort(msg)
        code.startTimer()
    }

}
